//初始化
$(function(){
		$('#dgDepartment').datagrid().datagrid('getPager');
		//$('#dg').datagrid().datagrid('getPager');
		$('#departmentWinId').window('close'); 
		$('#permissionWinId').window('close');
		$('#punishmentWinId').window('close'); 
		
		var type =typeid;
		$('#dgDepartment').datagrid({    
		    url:'../../department/queryDepartment',
		    rownumbers:true,
		    singleSelect:true,
		    pagination:true,
		    method:'post',
		    queryParams: {
		    	type:type
			},
		    columns:[[    
		        {field:'departmentCode',title:'部门编码',width:'28%',align:'center'},    
		        {field:'departmentName',title:'部门名称',width:'50%',align:'center'},    
		        {field:'isDel',title:'显示/隐藏',width:'20%',align:'center',
		        	formatter: function(value,row,index){
		        		var urlStats = 'departmentUrl';
						if(value=='1'){
							return '<span><a href="#" style="text-decoration:none;color:green" onclick=qidel('+row.id+','+value+',"'+urlStats+'"'+'); >已 显 示</a></span>';
						}
						if(value=='2'){
							return '<span><a href="#" style="text-decoration:none;color:red" onclick=qidel('+row.id+','+value+',"'+urlStats+'"'+'); >已 隐 藏</a></span>';
						}
					}
	
		        }    
		    ]]    
		});
		if(type==3 || type==4 || type==5){
			$("#dgDepartment").datagrid('hideColumn', 'departmentCode');
		}
		
		if(type==1){
			/*$("#permissionAddId").attr("onclick","permissionAddUpdate('add')");
			$("#permissionUpdateId").attr("onclick","permissionAddUpdate('edit')");
			$("#publicityHtmlId").html("许可公示");
			$('#dg').datagrid({    
			    url:'../../permission/queryPermission',
			    rownumbers:true,
			    singleSelect:true,
			    pagination:true,
			    method:'post',
			    columns:[[    
			        {field:'licensingAuthorityName',title:'所属部门',width:'20%',align:'center'},    
			        {field:'declareName',title:'申报者名称',width:'20%',align:'center'},    
			        {field:'declarationName',title:'申报名称',width:'30%',align:'center'},
			        {field:'permissionDate',title:'许可日期 ',width:'15%',align:'center',
			        	formatter: function(value){ 
			        		return formatDatebox(value);
			        	}
			        
			        },
			        {field:'isDel',title:'显示/隐藏',width:'10%',align:'center',
			        	formatter: function(value,row,index){ 
			        		return formatShowDel(value,row,index);
			        	}
			        },
			        {field:'declareNum',title:'申报号',width:'30%',align:'center'},
			        {field:'itemName',title:'事项名称',width:'30%',align:'center'},
			        {field:'doType',title:'办件类型',width:'10%',align:'center'},
			        {field:'certificateType',title:'申报者证件类型',width:'30%',align:'center'},
			        {field:'certificateNum',title:'申报者证件号码',width:'30%',align:'center'},
			        {field:'legalRepresentative',title:'法人代表',width:'30%',align:'center'},
			    ]]    
			});*/
		}
		if(type==2){
			/*$("#permissionAddId").attr("onclick","punishmentAddUpdate('add')");
			$("#permissionUpdateId").attr("onclick","punishmentAddUpdate('edit')");
			$("#publicityHtmlId").html("处罚公示");
			$('#dg').datagrid({    
			    url:'',
			    rownumbers:true,
			    singleSelect:true,
			    pagination:true,
			    method:'post',
			    columns:[[    
			        {field:'code',title:'所属部门',width:'20%',align:'center'},    
			        {field:'name',title:'行政相对人名称',width:'20%',align:'center'},    
			        {field:'price',title:'案件名称',width:'30%',align:'center'},
			        {field:'price',title:'处罚日期 ',width:'10%',align:'center'},
			        {field:'price',title:'显示/删除',width:'10%',align:'center'}
			    ]]    
			});*/
		}
	}) 
	//启用/删除
	function qidel(id,value,urlStats){
		var url = ""; 
		
		if("departmentUrl"==urlStats){
			url='../../department/updateDepartment';
		}
		if("permissionUrl"==urlStats){
			url='../../permission/updatePermission';
		}
	
		var isDel = "";
		var delName="";
		if(value=='1'){
			isDel='2';
			delName="隐藏";
		}
		if(value=='2'){
			isDel='1';
			delName="显示";
		} 
		$.messager.confirm('确认','您确认想要 [ '+delName+' ] 数据吗？',function(r){    
		    if (r){    
		    	$.ajax({  
					url:url,
					type:'post',
					data:{
					"id":id,
					"isDel":isDel
					},
					dataType:'json',
					error:function(){
						$.messager.alert("提示",delName+"异常","error");
					}, 
					success:function(map){ 
						var row = map.row;
						if(row>=0){
							$.messager.alert("提示",delName+"成功","info");
							if("departmentUrl"==urlStats){
								$('#dgDepartment').datagrid("reload");
							}
							if("permissionUrl"==urlStats){
								$('#dg').datagrid("reload");
							}
							return;
						}else{
							$.messager.alert("提示",delName+"失败","error");
							return;
						}
					}  
				});    
		    }    
		});
	}
//end

/* 部门添加或者修改 */
function departmentAddUpdat(states){
	if(states=="edit"){
		var row = $('#dgDepartment').datagrid('getSelected');
		if(row==null){
			$.messager.alert("提示","请先选中一条记录","error");
			return;
		}
		 $("#departmentCodeId").textbox('setValue', row.departmentCode);
		 $("#departmentNameId").textbox('setValue', row.departmentName);
	  	
	  	$('#departmentWinId').dialog({
			title: '修改部门信息',
			width: 350,
			height: 250,
			closed: false,
			cache: false,
			modal: true,
			buttons:[{
				text:'修改',
				iconCls:'icon-edit',
				handler:function(){
				  	if($("#addForm").form('validate')){
				  		var departmentCode = $("#departmentCodeId").val().replace(/\s/g, "");
				  		var departmentName = $("#departmentNameId").val().replace(/\s/g, "");
				  		if(typeid!=3 && typeid!=4 && typeid!=5 && departmentCode==""){
				  			$.messager.alert("友情提示","部门编码不允许为空","error");
				  			return;
				  		}
				  		if(departmentName==""){
				  			$.messager.alert("友情提示","部门名称不允许为空","error");
				  			return;
				  		}
				  		var typeId = typeid;
				  		$.ajax({  
							url:'../../department/updateDepartment',
							type:'post',
							data:{
							"id":row.id,
							"departmentCode":departmentCode,
							"departmentName":departmentName,
							"type":row.type
							},
							dataType:'json',
							error:function(){
								$.messager.alert("提示","修改异常","error");
							}, 
							success:function(map){ 
								var row = map.row;
								if(row>=0){
									$.messager.alert("提示","修改成功","info");
									$('#dgDepartment').datagrid("reload");
									$('#departmentWinId').dialog('close');
									return;
								}else{
									$.messager.alert("提示","添加失败","error");
									
									return;
								}
							}  
						});
				  	}
				    
				}
					
			},{
				text:'退出',
				iconCls:'icon-no',
				handler:function(){
					$('#departmentWinId').dialog('close');
				}
			}]
		});
		
	}
	if(states=="add"){
		 $("#departmentCodeId").textbox('setValue', "");
		 $("#departmentNameId").textbox('setValue', "");
			$('#departmentWinId').dialog({
				title: '新增部门信息',
				width: 350,
				height: 250,
				closed: false,
				cache: false,
				modal: true,
				buttons:[{
					text:'保存',
					iconCls:'icon-ok',
					handler:function(){
					  	if($("#addForm").form('validate')){
					  		var departmentCode = $("#departmentCodeId").val().replace(/\s/g, "");
					  		var departmentName = $("#departmentNameId").val().replace(/\s/g, "");
					  		if(typeid!=3 && typeid!=4 && typeid!=5 && departmentCode==""){
					  			$.messager.alert("友情提示","部门编码不允许为空","error");
					  			return;
					  		}
					  		if(departmentName==""){
					  			$.messager.alert("友情提示","部门名称不允许为空","error");
					  			return;
					  		}
					  		var typeId =typeid;
					  		$.ajax({  
								url:'../../department/adddDepartment',
								type:'post',
								data:{
								"departmentCode":departmentCode,
								"departmentName":departmentName,
								"type":typeId
								},
								dataType:'json',
								error:function(){
									$.messager.alert("提示","添加异常","error");
								}, 
								success:function(map){ 
									var row = map.row;
									if(row>=0){
										$.messager.alert("提示","添加成功","info");
										$('#dgDepartment').datagrid("reload");
										$('#departmentWinId').dialog('close');
										return;
									}else{
										$.messager.alert("提示","添加失败","error");
										
										return;
									}
								}  
							});
					  	}
					    
					}
						
				},{
					text:'退出',
					iconCls:'icon-no',
					handler:function(){
						$('#departmentWinId').dialog('close');
					}
				}]
			});
		
	}
}
	

/* 公告添加或者修改 */
function permissionAddUpdate(states){
	//许可时间选择框
	$('#permissionDateId').datebox({    
	    required:true,
	    width:250,
	    editable:false
	});
	//部门下拉框加载和选择默认值
	$('#licensingAuthorityId').combobox({    
	    url:'../../department/queryDepartmentCombobox',
	    queryParams: {
			"type" : '1'
		},
	    valueField:'departmentCode',    
	    textField:'departmentName',
	    DropDownStyle:'DropDownList'/*,
	    onLoadSuccess: function () { //加载完成后,设置选中第一项
            var val = $(this).combobox("getData");
            for (var item in val[0]) {
                if (item == "departmentCode") {
                    $(this).combobox("select", val[0][item]);
                }
            }
        }*/ 
	});
	if(states=="edit"){
		var row = $('#dg').datagrid('getSelected');
		if(row==null){
			$.messager.alert("提示","请先选中一条记录","error");
			return;
		}
		 $("#declareNumId").textbox('setValue', row.declareNum);
		 $("#declarationNameId").textbox('setValue', row.declarationName);
		 $("#itemNameId").textbox('setValue', row.itemName);
		 $("#doTypeId").textbox('setValue', row.doType);
		 $("#declareNameId").textbox('setValue', row.declareName);
		 $("#certificateTypeId").textbox('setValue', row.certificateType);
		 $("#certificateNumId").textbox('setValue', row.certificateNum);
		 $("#legalRepresentativeId").textbox('setValue', row.legalRepresentative);
		 $("#permissionDateId").datebox('setValue',formatDatebox(row.permissionDate));
		 $("#licensingAuthorityId").combobox("select", row.licensingAuthority);
		 
	  	$('#permissionWinId').dialog({
			title: '修改许可公示信息',
			width: 500,
			height: 430,
			closed: false,
			cache: false,
			modal: true,
			buttons:[{
				text:'修改',
				iconCls:'icon-edit',
				handler:function(){
				  	if($("#permissionForm").form('validate')){
				  		var updateUrl = '../../permission/updatePermission';
				  		permissionAddOrUpdateYZ(updateUrl,"修改",row.id);
				  		$('#dg').datagrid("reload");
				  	} 
				}
					
			},{
				text:'退出',
				iconCls:'icon-no',
				handler:function(){
					$('#permissionWinId').dialog('close');
				}
			}]
		});
		
	}
	if(states=="add"){
		$('#permissionForm').form('clear');
			$('#permissionWinId').dialog({
				title: '新增部门信息',
				width: 500,
				height: 430,
				closed: false,
				cache: false,
				modal: true,
				buttons:[{
					text:'保存',
					iconCls:'icon-ok',
					handler:function(){
					  	if($("#permissionForm").form('validate')){
					  		var addUrl = '../../permission/addPermission';
					  		permissionAddOrUpdateYZ(addUrl,"添加",0);
					  		$('#dg').datagrid("reload");
					  	}
					    
					}
						
				},{
					text:'退出',
					iconCls:'icon-no',
					handler:function(){
						$('#permissionWinId').dialog('close');
					}
				}]
			});
	}
	
}
//许可公示添加或修改验证输入的值
function permissionAddOrUpdateYZ(url,stateStr,id){
		var declareNum = $("#declareNumId").val().replace(/\s/g, "");
		var declarationName = $("#declarationNameId").val().replace(/\s/g, "");
		var itemName = $("#itemNameId").val().replace(/\s/g, "");
		var doType = $("#doTypeId").val().replace(/\s/g, "");
		var declareName = $("#declareNameId").val().replace(/\s/g, "");
		var legalRepresentative = $("#legalRepresentativeId").val().replace(/\s/g, "");
		var licensingAuthority = $("#licensingAuthorityId").combobox('getValue').replace(/\s/g, "");
		var certificateType = $("#certificateTypeId").val().replace(/\s/g, "");
		var certificateNum = $("#certificateNumId").val().replace(/\s/g, "");
		var permissionDate = $("#permissionDateId").datebox('getValue').replace(/\s/g, "");
		if(declareNum==""){
			$.messager.alert("友情提示","申报号不允许为空","error");
			return false;
		}
		if(declarationName==""){
			$.messager.alert("友情提示","申报名称不允许为空","error");
			return false;
		}
		if(itemName==""){
			$.messager.alert("友情提示","事项名称不允许为空","error");
			return false;
		}
		if(doType==""){
			$.messager.alert("友情提示","办证类型不允许为空","error");
			return false;
		}
		if(declareName==""){
			$.messager.alert("友情提示","申报者名称不允许为空","error");
			return false;
		}
		if(legalRepresentative==""){
			$.messager.alert("友情提示","法人代表不允许为空","error");
			return false;
		}
		if(permissionDate==""){
			$.messager.alert("友情提示","许可日期不允许为空","error");
			return false;
		}
		
		if(licensingAuthority==""){
			$.messager.alert("友情提示","许可机关不允许为空","error");
			return false;
		}
		$.ajax({  
			url:url,
			type:'post',
			data:{
			"id":id,
			"declareNum":declareNum,
			"declarationName":declarationName,
			"itemName":itemName,
			"doType":doType,
			"declareName":declareName,
			"legalRepresentative":legalRepresentative,
			"licensingAuthority":licensingAuthority,
			"certificateType":certificateType,
			"certificateNum":certificateNum,
			"permissionDateStr":permissionDate
			},
			dataType:'json',
			error:function(){
				$.messager.alert("提示",stateStr+"异常","error");
			}, 
			success:function(map){ 
				var row = map.row;
				if(row>=0){
					$.messager.alert("提示",stateStr+"成功","info");
					$('#dgDepartment').datagrid("reload");
					return;
				}else{
					$.messager.alert("提示",stateStr+"失败","error");
					return;
				}
			}  
		});
		
}
//处罚公示的添加和修改
function punishmentAddUpdate(states){
	//许可时间选择框
	$('#punishmentDateStrId').datebox({    
	    required:true,
	    width:250,
	    editable:false
	});
	//部门下拉框加载和选择默认值
	$('#punishmentOrganId').combobox({    
	    url:'../../department/queryDepartmentCombobox',
	    queryParams: {
			"type" : '2'
		},
	    valueField:'departmentCode',    
	    textField:'departmentName',
	    DropDownStyle:'DropDownList' 
	});
	
	if(states=="edit"){
		var row = $('#dg').datagrid('getSelected');
		if(row==null){
			$.messager.alert("提示","请先选中一条记录","error");
			return;
		}
	  	$('#punishmentWinId').dialog({
			title: '修改处罚公示信息',
			width: 500,
			height: 430,
			closed: false,
			cache: false,
			modal: true,
			buttons:[{
				text:'修改',
				iconCls:'icon-edit',
				handler:function(){
				  	if($("#punishmentForm").form('validate')){
				  		/*var updateUrl = '../../permission/updatePermission';
				  		permissionAddOrUpdateYZ(updateUrl,"修改",row.id);*/
				  		$('#dg').datagrid("reload");
				  	} 
				}
					
			},{
				text:'退出',
				iconCls:'icon-no',
				handler:function(){
					$('#punishmentWinId').dialog('close');
				}
			}]
		});
		
	}
	if(states=="add"){
		$('#punishmentForm').form('clear');
			$('#punishmentWinId').dialog({
				title: '新增处罚公示信息',
				width: 500,
				height: 430,
				closed: false,
				cache: false,
				modal: true,
				buttons:[{
					text:'保存',
					iconCls:'icon-ok',
					handler:function(){
					  	if($("#punishmentForm").form('validate')){
					  		var addUrl = '../../punishment/addPunishment';
					  		punishmentAddOrUpdateYZ(addUrl,"添加",0);
					  		$('#dg').datagrid("reload");
					  	}
					    
					}
						
				},{
					text:'退出',
					iconCls:'icon-no',
					handler:function(){
						$('#punishmentWinId').dialog('close');
					}
				}]
			});
	}
}

//处罚公示添加或修改验证输入的值
function punishmentAddOrUpdateYZ(url,stateStr,id){
		var punishmentNum = $("#punishmentNumId").val().replace(/\s/g, "");
		var caseName = $("#caseNameId").val().replace(/\s/g, "");
		var administrativeName = $("#administrativeNameId").val().replace(/\s/g, "");
		var administrativeCertificateType = $("#administrativeCertificateTypeId").val().replace(/\s/g, "");
		var administrativeCertificateNum = $("#administrativeCertificateNumId").val().replace(/\s/g, "");
		var punishmentDateStr = $("#punishmentDateStrId").datebox('getValue').replace(/\s/g, "");
		var punishmentOrgan = $("#punishmentOrganId").combobox('getValue').replace(/\s/g, "");
		var punishmentText = $("#punishmentTextId").val().replace(/\s/g, "");
		
		if(punishmentNum==""){
			$.messager.alert("友情提示","行政处罚决定书文号不允许为空","error");
			return false;
		}
		if(caseName==""){
			$.messager.alert("友情提示","案件名称不允许为空","error");
			return false;
		}
		if(administrativeName==""){
			$.messager.alert("友情提示","行政相对人名称不允许为空","error");
			return false;
		}
		if(administrativeCertificateType==""){
			$.messager.alert("友情提示","行政相对人证件类型不允许为空","error");
			return false;
		}
		if(administrativeCertificateNum==""){
			$.messager.alert("友情提示","行政相对人证件号码不允许为空","error");
			return false;
		}
		if(punishmentDateStr==""){
			$.messager.alert("友情提示","处罚日期不允许为空","error");
			return false;
		}
		if(punishmentOrgan==""){
			$.messager.alert("友情提示","处罚机关不允许为空","error");
			return false;
		}
		
		if(punishmentText==""){
			$.messager.alert("友情提示","处罚决定书或摘要不允许为空","error");
			return false;
		}
		$.ajax({  
			url:url,
			type:'post',
			data:{
			"id":id,
			"punishmentNum":punishmentNum,
			"caseName":caseName,
			"administrativeName":administrativeName,
			"administrativeCertificateType":administrativeCertificateType,
			"administrativeCertificateNum":administrativeCertificateNum,
			"punishmentDateStr":punishmentDateStr,
			"punishmentOrgan":punishmentOrgan,
			"punishmentText":punishmentText
			},
			dataType:'json',
			error:function(){
				$.messager.alert("提示",stateStr+"异常","error");
			}, 
			success:function(map){ 
				var row = map.row;
				if(row>=0){
					$.messager.alert("提示",stateStr+"成功","info");
					$('#dgDepartment').datagrid("reload");
					return;
				}else{
					$.messager.alert("提示",stateStr+"失败","error");
					return;
				}
			}  
		});
		
}

/* 对	显示/隐藏  进行 format */
function formatShowDel(value,row,index){
	
	if (value == null || value == '') {  
	        return '';  
	}
	var urlStats = "permissionUrl";
	if(value=="1"){
		
		return '<span><a href="#" style="text-decoration:none;color:green" onclick=qidel('+row.id+','+value+',"'+urlStats+'"'+'); >已 显 示</a></span>';
	}
	if(value=="2"){
		return '<span><a href="#"  style="text-decoration:none;color:red " onclick=qidel('+row.id+','+value+',"'+urlStats+'"'+'); >已 隐 藏</a></span>';
	}
}

	
/*对创建时间format  */	
function formatDatebox(value) {  
    if (value == null || value == '') {  
        return '';  
    }  
    var dt;  
    if (value instanceof Date) {  
        dt = value;  
    } else {  
        dt = new Date(value);  
    }  
  
    return dt.format("yyyy-MM-dd"); //扩展的Date的format方法(上述插件实现)  
}
	
	Date.prototype.format = function (format) {  
	    var o = {  
	        "M+": this.getMonth() + 1, // month  
	        "d+": this.getDate(), // day  
	        "h+": this.getHours(), // hour  
	        "m+": this.getMinutes(), // minute  
	        "s+": this.getSeconds(), // second  
	        "q+": Math.floor((this.getMonth() + 3) / 3), // quarter  
	        "S": this.getMilliseconds()  
	        // millisecond  
	    }  
	    if (/(y+)/.test(format))  
	        format = format.replace(RegExp.$1, (this.getFullYear() + "")  
	            .substr(4 - RegExp.$1.length));  
	    for (var k in o)  
	        if (new RegExp("(" + k + ")").test(format))  
	            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));  
	    return format;  
} 
